package is.glif.fenius.translators.argia.db;


import is.glif.fenius.translators.argia.beans.Reservation;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 
 * @author Ali LAHLOU (Synchromedia, ETS)
 *
 */
public class ReservationDAO {
	
	public void create(Reservation rsv) { 
        Session session = HibernateSessionUtil.getSessionfactory().openSession(); 
        session.getTransaction().begin(); 
        
        System.out.println("saving: "+rsv.getArgiaID());
        session.save(rsv);
        
        session.getTransaction().commit(); 
    } 
    
    @SuppressWarnings("unchecked")
    public List<Reservation> findAll() { 
        Session session = HibernateSessionUtil.getSessionfactory().openSession(); 
        List<Reservation> list = (List<Reservation>) session.createQuery("From Reservation").list(); 
        return list;
    }
    
    public Reservation find(String feniusID) { 
        Session session = HibernateSessionUtil.getSessionfactory().openSession(); 
        Query q = session.createQuery("from Reservation res where res.feniusID = :id");
        q.setParameter("id", feniusID);
        List<Reservation> list = (List<Reservation>) q.list();
        if(list.size() > 0){
        	return list.get(0);
        }
        return null;
    }
    
    public void update(Reservation rsv){
    	
    	 Session session = HibernateSessionUtil.getSessionfactory().openSession(); 
         session.getTransaction().begin(); 
         
         System.out.println("updating: "+rsv.getArgiaID());
         session.update(rsv);
         
         session.getTransaction().commit(); 
    	
    }

}
